/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.insurance.cis.utils;

import com.insurance.cis.connection.CisConnection;
import com.insurance.cis.connection.JdbcConfig;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author TruongLQ
 */
public final class Utils {

    private Utils() {
        throw new AssertionError("Can not initialize Utils class");
    }

    public static JdbcConfig loadJdbcConfig() {
        final JdbcConfig jdbcConfig = new JdbcConfig();
        final Properties properties = new Properties();
        try {
            properties.load(new FileInputStream(JdbcConfig.CONFIG_FILE));
            jdbcConfig.setDriverClass(properties.getProperty(JdbcConfig.DRIVER_CLASS));
            jdbcConfig.setProtocolPrefix(properties.getProperty(JdbcConfig.PROTOCOL_PREFIX));
            jdbcConfig.setProtocolHost(properties.getProperty(JdbcConfig.PROTOCOL_HOST));
            jdbcConfig.setProtocolPort(properties.getProperty(JdbcConfig.PROTOCOL_PORT));
            jdbcConfig.setDatabaseName(properties.getProperty(JdbcConfig.DATABASE_NAME));
            jdbcConfig.setUsername(properties.getProperty(JdbcConfig.USERNAME));
            jdbcConfig.setPassword(properties.getProperty(JdbcConfig.PASSWORD));
        } catch (IOException ex) {
            Logger.getLogger(JdbcConfig.class.getName()).log(Level.SEVERE, null, ex);
        }
        return jdbcConfig;
    }

    public static void saveJdbcConfig(final JdbcConfig jdbcConfig) {
        try {
            try (BufferedWriter bw = new BufferedWriter(new FileWriter(JdbcConfig.CONFIG_FILE))) {
                bw.write(jdbcConfig.toString());
                bw.close();
            }
        } catch (IOException ex) {
            Logger.getLogger(Utils.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static boolean checkConnection() {
        try {
            Connection connection = CisConnection.getInstance().getSqlConnection();
            return (connection != null);
        } catch (SQLException ex) {
            String msg = "Could not establish connection to database.\n";
            MessageDialogUtils.dialogError(null, msg);
        }
        return false;
    }

    public static String convertDateToString(Date date, String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(date);
    }

    public static Date parseDate(String date, String pattern) throws ParseException {
        DateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.parse(date);
    }
    
    public static String formatWithHHMMSS(Date date, String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern+" HH:mm:ss");
        return sdf.format(date);
    }
}
